import sys
sys.path.append('../../')
import BasicLibraries   as BL 
import Make_Population
import global_variables as GV
from Functions.Miscellaneous import utils as ut
import regressions
import warnings
from statsmodels.tools.sm_exceptions import ConvergenceWarning
warnings.simplefilter('ignore', ConvergenceWarning)
warnings.filterwarnings("ignore", category=FutureWarning)
warnings.filterwarnings("ignore", category=RuntimeWarning)


marker_type, line_style, index_dict, abb_index_dict, inverse_dict, STcontrols = GV.global_vars()
MARGINAL=False

############################
data = Make_Population.MakeUniverse()
empiricsBS = regressions.Analysis()
ROB = regressions.RobustnessTest()
############################

'''
This is the code to reproduce the full sample (non-bootrapped)results: Table 6 and 7 in the SI

The code is organised as follow:
    STEP 1. Load the datasets
    STEP 2. Estimate the regression coefficients
    STEP 3. Plot them
    
Note that running this file requires access to several datasets as describe in the main text and the README file

'''


#=========== STEP 1 ============

#%% Fundamentals + dummies
compustat, regions, sector, tc = data.make_financials()
compustat_clean = compustat.copy()

#%% This is our derived measure of O-ST (the dataset is available in the folder)
system_thinking = BL.pd.read_csv('local_data/OST.csv')
id_mapping = BL.pd.read_csv('local_data/ISIN_gvkey_mapping.csv')[['ISIN', 'gvkey']]
system_thinking = system_thinking.merge(id_mapping)


#%%  These are the data you need for the Heckman correction step
sector_firms = compustat_clean[compustat_clean.GICS_level_1.isin(['Energy', 'Industrial', 'Material', 'Utilities'])]
cdp_list = BL.pickle.load( open('local_data/CDP_GVKEY_map.pckl', 'rb'))
cdp_firms = sector_firms[sector_firms.gvkey.isin(cdp_list.gvkey)]
non_cdp_firms = sector_firms[sector_firms.gvkey.isin(cdp_list.gvkey)==False]

#%% Paris data
MAX_ProjectionYear, paris2018,paris2019,paris2020, base_dummies = data.get_paris_for_analysis('below')

#%% Other esg
ref, vars_, cdp_t, ref_t = data.make_other_esg(system_thinking, version_ = '_v2')
#%%
mtb = BL.pd.read_csv('local_data/refinitiv_mtb.csv')
compustat = compustat.merge(mtb[['MTB', 'mrg']])


#=========== STEP 2 A Emissions ============


#%% What explain system thinking?
g, dummies = empiricsBS.get_explanators_dt(cdp_t, ref_t, system_thinking[system_thinking.Year<=2019], compustat[compustat.fyear <= 2019], sector, regions, ref, tc,  vars_, cdp_firms, non_cdp_firms,robustness=False, ratio = 1)
dt_expl, expl, relevant_expl = empiricsBS.Explanators(g,dummies)
expl = expl.rename(index = index_dict)
print(expl.to_latex(escape = False))
#%% Which policies are associated with ST
g, explanators, other_esg,   dummies = empiricsBS.get_mediators_dt(cdp_t, ref_t, system_thinking[system_thinking.Year<=2019], compustat[compustat.fyear <= 2019], sector, regions, ref, tc, relevant_expl,  vars_, marginal_=MARGINAL, robustness=False, ratio = 1)
dt_med, meds, relevant_mediators = empiricsBS.mediators(g, explanators, other_esg,  dummies)
meds = meds.rename(index = index_dict)
print(meds.to_latex(escape = False))

#%% Explain emissions
rob_choice_ = 0
addVARS = [None, ['Activity_type'], ['Activity_type', 'number_of_goals']][rob_choice_]
g, explanators, other_esg,  dummies = empiricsBS.get_emissions_dt(cdp_t, ref_t, system_thinking[system_thinking.Year<=2019], compustat[compustat.fyear <= 2019], sector, regions, ref, tc, relevant_expl,  relevant_mediators, cdp_firms, non_cdp_firms, robustness=False, ratio = 1)
dt_ghg, ghg_outcome, ghg_models = empiricsBS.Emissions(g, explanators, other_esg,  dummies, additional_vars = addVARS)
ghg_outcome = ghg_outcome.rename(index = index_dict)
ghg_outcome.columns = ['Without CSR policies', 'With CSR policies']
print(ghg_outcome.to_latex(escape = False))
rs = ut.utils().make_qqPlot(ghg_models[0].resid)
BL.plt.savefig('Figures/GHGResiduals.pdf', dpi = 150, bbox_inches = 'tight')

#=========== STEP 2 B Alignment ============

#%% What explain system thinking?
g, dummies = empiricsBS.get_explanators_dt(cdp_t, ref_t, system_thinking, compustat, sector, regions, ref, tc,  vars_, cdp_firms, non_cdp_firms,robustness=False, ratio = 1)
dt_expl, expl, relevant_expl = empiricsBS.Explanators(g,dummies)
expl = expl.rename(index = index_dict)
print(expl.to_latex(escape = False))
#%% Which policies are associated with ST
g, explanators, other_esg,   dummies = empiricsBS.get_mediators_dt(cdp_t, ref_t, system_thinking, compustat, sector, regions, ref, tc, relevant_expl,  vars_, marginal_=MARGINAL, robustness=False, ratio = 1)
dt_med, meds, relevant_mediators = empiricsBS.mediators(g, explanators, other_esg,  dummies)
meds = meds.rename(index = index_dict)
print(meds.to_latex(escape = False))
#%% Explain Paris
rob_choice_ = 0 # if 1 or 2 then add the simpler measure of O-St as in table S11
addVARS = [None, ['Activity_type'], ['Activity_type', 'number_of_goals']][rob_choice_]
dt, explanators, other_esgs, dummies, base_dummies, did_it_run = empiricsBS.get_Paris_dt(paris2018, paris2019, paris2020, system_thinking, compustat, sector, regions, ref, ref_t, tc,relevant_expl, relevant_mediators, base_dummies, MAX_ProjectionYear, marginal_=MARGINAL, robustness=False, ratio = 0.9)
dt_paris, Paris_model = empiricsBS.ParisAgreement(dt, explanators, other_esgs, dummies, base_dummies, additional_vars = addVARS)
Paris_model = Paris_model.rename(index = index_dict)
Paris_model.columns = ['Without CSR policies', 'With CSR policies']
print(Paris_model.to_latex(escape = False))

#%% Explain Paris
rob_choice_ = 0 # if 1 or 2 then add the simpler measure of O-St as in table S11
addVARS = [None, ['Activity_type'], ['Activity_type', 'number_of_goals']][rob_choice_]
MAX_ProjectionYear, paris2018WELL,paris2019WELL,paris2020WELL, base_dummiesWELL = data.get_paris_for_analysis('well_below')
dt, explanatorsWELL, other_esgsWELL, dummiesWELL, base_dummiesWELL, did_it_runWELL = empiricsBS.get_Paris_dt(paris2018WELL, paris2019WELL, paris2020WELL, system_thinking, compustat, sector, regions, ref, ref_t, tc,relevant_expl, relevant_mediators, base_dummiesWELL, MAX_ProjectionYear, marginal_=MARGINAL, robustness=False, ratio = 0.9)
dt_parisWELL, Paris_modelWELL = empiricsBS.ParisAgreement(dt, explanators, other_esgs, dummies, base_dummies, additional_vars = addVARS)
Paris_modelWELL = Paris_modelWELL.rename(index = index_dict)
Paris_modelWELL.columns = ['Without CSR policies', 'With CSR policies']
print(Paris_modelWELL.to_latex(escape = False))

#%% Print out the tables
dx = BL.pd.concat(dict(Emissions=ghg_outcome, Alignment=Paris_model, AlignmentWELL=Paris_modelWELL), axis = 1)
print(dx.to_latex(escape = False).replace('{2}{l}', '{2}{c}').\
      replace('{Alignment}', '{Alignment below 2$^\circ$C}').\
          replace('{AlignmentWELL}', '{Alignment well below 2$^\circ$C}').\
              replace('{lllllll}', '{l|ll|ll|ll}').\
                  replace('Activity_type', 'Number of projects').\
                      replace('number_of_goals', 'Number of objectives').\
              replace('System Thinking', r'{\bf System Thinking}').replace('NaN', ''))
    
    

